//
//  YYSearchView.m
//  YYSearchView
//
//  Created by mac on 16/7/12.
//  Copyright © 2016年 Jack YY. All rights reserved.
//

#import "CWQ_searchView.h"
#import "NSString+Utilities.h"

@implementation CWQ_searchView

-(void)awakeFromNib
{
//    self.YYBgView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"navi"]];
    wsj_cornerRadius(self.searchBtn, 5);
    self.YCWQSearch.leftView = [self leftView];
    self.YCWQSearch.delegate = self;
    self.YCWQSearch.tintColor = kMainColor;
    self.YCWQSearch.leftViewMode = UITextFieldViewModeAlways;
    [super awakeFromNib];
}

+(instancetype)creatView
{

   CWQ_searchView  *v = [[[NSBundle mainBundle]loadNibNamed:@"CWQ_searchView" owner:nil options:nil]lastObject];
  
    return v;
}

-(UIView *)leftView
{
    UIView * leftView = [[UIView alloc]initWithFrame:CGRectMake(18, 8, 24, 24)];
    UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(8, 4, 16, 16);
    [button setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
//    [button addTarget:self action:@selector(searchTile) forControlEvents:UIControlEventTouchUpInside];
    [leftView addSubview:button];
    return leftView;
}

-(void)searchTile
{
    NSString *str = self.YCWQSearch.text;
    
    
   
    if (!str || [str isEqualToString:@""]) {
        alertMessage(@"请输入关键字");
        return;
    }
    NSString *handleStr = [str handleSpaceStr] ;
    if ([handleStr isEqualToString:@""]) {
         alertMessage(@"请输入关键字");
    }
   
    
    [self.YCWQSearch resignFirstResponder];
    //去掉首尾空格
    self.CWQGetTitle(handleStr);
}

- (IBAction)search:(id)sender {
    [self searchTile];
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return  YES;
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    
    
    [self searchTile];
    return YES;
    
}

@end
